package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsGatewayTemplate;
import com.caiyi.financial.nirvana.sms.querybean.GatewayTemplateQueryBean;
import com.caiyi.financial.nirvana.sms.vo.SmsGatewayTemplateVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface SmsGatewayTemplateMapper extends BaseMapper<SmsGatewayTemplate> {


    @SelectProvider(type = GatewayTemplateSqlProvider.class, method = "selectGatewayTemplateListByParams")
    List<SmsGatewayTemplateVo> selectGatewayTemplateListByParams(GatewayTemplateQueryBean bean);

    class GatewayTemplateSqlProvider {
        public String selectGatewayTemplateListByParams(GatewayTemplateQueryBean bean) {
            return new SQL() {
                {
                    SELECT("s.id,g.name,s.template,s.create_time as createTime,s.update_time as updateTime");
                    FROM("sms_gateway_template s");
                    LEFT_OUTER_JOIN("sms_gateway g on s.gateway_id=g.id");
                    if (!StringUtils.isEmpty(bean.getName())) {
                        WHERE("g.name LIKE concat('%',#{name},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getTemplate())) {
                        WHERE("s.template LIKE concat('%',#{template},'%')");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("s.create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("s.create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }
    }
}
